package com.mikey.pc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author :Mikey
 * @description :精准线程唤醒
 * @create :2022-09-20 14:51:00
 */
public class C {
    public static void main(String[] args) {
        Date2 date2 = new Date2();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                date2.printA();
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                date2.printB();
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                date2.printC();
            }
        },"C").start();
    }
}
class Date2{
    private Lock lock=new ReentrantLock();
    private Condition condition1=lock.newCondition();
    private Condition condition2=lock.newCondition();
    private Condition condition3=lock.newCondition();
    private int num=1;//1A,2B,3C
    public void printA(){
        lock.lock();
        try {
            //业务，判断
            while (num!=1){
                condition1.await();
            }
            System.out.println(Thread.currentThread().getName()+"->AAAA");
            num = 2;
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public void printB(){
        lock.lock();
        try {
            //业务，判断
            while (num!=2){
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName()+"->BBBB");
            num = 3;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    public void printC(){
        lock.lock();
        try {
            //业务，判断
            //业务，判断
            while (num!=3){
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName()+"->CCCC");
            num = 1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
    //生产线：下单-支付-发货
}
